﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.Utils.Menu;
using DevExpress.XtraEditors;
using ERP_RORZE_DLL.Product.DAO;
using ERP_RORZE_DLL.Product.database;
using ERP_RORZE_DLL.Product.Base;
using ERP_RORZE_PERMISSION.Base;
using Utilities = ERP_RORZE_TOOL.Common.Utilities;

namespace ERP_RORZE_PRODUCT.Product.PartHandling
{
    public partial class ctrlAnodInspDetail : DevExpress.XtraEditors.XtraUserControl
    {
        ClsPartHandlingDAO clsParthandinhDAO = new ClsPartHandlingDAO();
        DataTable dt;
        ClsDAnodInspTable obj = new ClsDAnodInspTable();
        DXPopupMenu menue;
        private LogUtils log;
        DXPopupMenu menu;

        public ctrlAnodInspDetail()
        {
            InitializeComponent();

            InitDateRangeMenu();
            InitPopupMenu();
            this.dropbtDateRange.DropDownControl = menu;
            dropbtExport.DropDownControl = menue;
            log = new LogUtils();
            this.dropbtDateRange.Text = "6 Months";
        }

        private void ctrlAnodInspDetail_Load(object sender, EventArgs e)
        {
            Re_load(sender, e);
        }
        void Re_load(object sender, EventArgs e)
        {
            BindDatatoGrid();
        }
        private void btnReload_Click(object sender, EventArgs e)
        {
            Re_load(sender, e);
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn có thực sự muốn xóa dữ liệu!", "Message", MessageBoxButtons.OKCancel,
                                MessageBoxIcon.Question) == DialogResult.OK)
            {
                obj.ID = int.Parse(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "ID").ToString());
                clsParthandinhDAO.DeleteDAnodInspDetail(obj);

                //write Log
                log.Table = LogConstant.Table.Product.Anod_Insp;
                log.Task = LogConstant.Task.Delete;
                log.Description = "MONo: " + gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "MONo") + "; Date: " + gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Date");
                log.WriteLog();

            }
            Re_load(sender, e);
        }

        private void btnInput_Click(object sender, EventArgs e)
        {
            frm_AnodInspInput frmAdd = new frm_AnodInspInput();
            frmAdd.Reload += new EventHandler(Re_load);
            frmAdd.Show();
        }

        private void gridControl1_Load(object sender, EventArgs e)
        {
            Re_load(sender, e);
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            Re_load(sender, e);
        }

        private void btnAddNew_Click(object sender, EventArgs e)
        {
            btnInput_Click(sender, e);
        }

        private void btDelete_Click(object sender, EventArgs e)
        {
            btnDelete_Click(sender, e);
        }

        private void btnExport_Click(object sender, EventArgs e)
        {
            clsConstantsFuction.ExportToExcel(gridView1, 1, true);
        }

        //Date range
        protected virtual void InitDateRangeMenu()
        {
            menu = new DXPopupMenu();
            menu.Items.Add(new DXMenuItem("One Month", new EventHandler(OnItemClick1Month), null, null, null, null));
            menu.Items.Add(new DXMenuItem("6 Months", new EventHandler(OnItemClick6Months), null, null, null, null));
            menu.Items.Add(new DXMenuItem("One Year", new EventHandler(OnItemClick1Year), null, null, null, null));
            menu.Items.Add(new DXMenuItem("All time", new EventHandler(OnItemClickAllTime), null, null, null, null));

        }

        void OnItemClick1Month(object sender, EventArgs e)
        {
            BindDatatoGrid(1);
            dropbtDateRange.Text = "One Month";
        }

        void OnItemClick6Months(object sender, EventArgs e)
        {
            BindDatatoGrid(2);
            dropbtDateRange.Text = "6 Months";
        }

        void OnItemClick1Year(object sender, EventArgs e)
        {
            BindDatatoGrid(3);
            dropbtDateRange.Text = "One Year";
        }

        void OnItemClickAllTime(object sender, EventArgs e)
        {
            BindDatatoGrid(0);
            dropbtDateRange.Text = "All time";
        }

        private void BindDatatoGrid(int range = 2)
        {

            Cursor.Current = Cursors.WaitCursor;
            dt = clsParthandinhDAO.GetAllAnodInspDetail(range);
            gridControl1.DataSource = dt;
            gridView1.BestFitColumns();
            Cursor.Current = Cursors.Default;
        }

        //Export to excel button

        protected virtual void InitPopupMenu()
        {
            menue = new DXPopupMenu();
            menue.Items.Add(new DXMenuItem("Export all", new EventHandler(OnItemClickExportAll), null, null, null, null));
            menue.Items.Add(new DXMenuItem("Export selected row(s)", new EventHandler(OnItemClickExportRows), null, null, null, null));

        }
        void OnItemClickExportAll(object sender, EventArgs e)
        {
            Utilities.ExportToExcel(gridView1, 1, true);
        }
        void OnItemClickExportRows(object sender, EventArgs e)
        {
            Utilities.ExportToExcel(gridView1, 0, true);
        }
    }
}
